﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrameWorkSong;
using System.Linq;

public class AssetBundleManifestExamples : MonoBehaviourSimplify
{
    enum mode
    {
        readMF,//读取依赖
        noReadMF//不读取依赖
    }
    ResLoader ResLoader = new ResLoader();
    // Start is called before the first frame update
    void Start()
    {
       // ReadManifest();
        GetAssetBundles(mode.readMF, "gameobject");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    AssetBundle LoadAB(string abName)
    {
       // var mianAB = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abName);
        var mianAB = ResLoader.LoadAssetSync<AssetBundle>(abName, Application.streamingAssetsPath + "/"+ abName);
        return mianAB;
    }
    GameObject LoadOBJ(string abName,string ObjName)
    {
        var mianAB = ResLoader.LoadAssetSync<GameObject>(abName, ObjName, Application.streamingAssetsPath + "/" + abName);
        return mianAB;
    }
    AssetBundleManifest ReadMfText()
    {
        var mianAB = LoadAB("StandaloneWindows");
        var bundleManifest = mianAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        return bundleManifest;
    }

    //依赖阅读
    void ReadManifest()
    {

        var bundleManifest = ReadMfText();
        //查询所有依赖（遍历所有）
        bundleManifest.GetAllDependencies("gameobject")
            .ToList()
            .ForEach(assetBunle => { Debug.Log(assetBunle); });
        //查询AssetBundles
        bundleManifest.GetAllAssetBundles()
            .ToList()
            .ForEach(assetBunle => { Debug.Log(assetBunle); });
        //查询直接依赖（只遍历一层）
        bundleManifest.GetDirectDependencies("gameobject")
           .ToList()
           .ForEach(assetBunle => { Debug.Log(assetBunle); });
    }
    //同步加载依赖
    void GetAssetBundles(mode modeMF, string abName)
    {
        var bundleManifest = ReadMfText();
        switch (modeMF)
        {
            case mode.readMF:
                bundleManifest.GetAllDependencies(abName)
        .ToList()
        .ForEach(assetBunle =>
        {
            LoadAB(assetBunle);
           
        });
                //var ab = LoadAB(abName);
                //var abManifest = ab.LoadAsset<AssetBundleManifest>(abName);
                // GameObject game = ab.LoadAsset<GameObject>("Game");

                GameObject game = LoadOBJ(abName, "Game");
                Instantiate(game);
                
                break;
            case mode.noReadMF:
                var ab1 = LoadAB(abName);
                GameObject game1 = ab1.LoadAsset<GameObject>("Game");
                Instantiate(game1);
                break;
            default:
                break;
        }
    }


    public override void OnBeforeDestroy()
    {
       // ResLoader.ReleaseAllRecord();
    }
}
